// gerenInfo/pages/phoneNum/phoneNum.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    new_phoneNum: '', // 新手机号
    error: false, // 错误提示
    color: 0, // 按钮颜色 0灰色 1蓝色
    codeFlag: false,
    isNum: '',
    code: '', //验证码
    num: 60, //倒计时
    old_phoneNum: '',
  },

  // 获取验证码
  getCode() {
    var reg_tel =
      /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
      new_phoneNum = this.data.new_phoneNum,
      error = this.data.error;
    if (new_phoneNum == '' || !reg_tel.test(new_phoneNum)) {
      error = true;
      this.setData({
        error
      })
      return false;
    } else if (new_phoneNum == this.data.old_phoneNum) {
      wx.showToast({
        title: '请输入新的手机号',
        icon: 'none'
      })
      return
    }

    wx.showLoading({
      title: '获取中',
    })

    var that = this,
      url = app.globalData.url + 'member/getCode',
      telephone = '',
      error = false;

    app.wxRequest(url, {
      telNum: new_phoneNum
    }, res => {
      console.log(res);
      if (res.flag) {
        // 提取字符串某部分: 起始索引（开始位置），终止索引（结束位置）
        telephone = new_phoneNum.substring(0, 3) + '****' + new_phoneNum.substring(7);
        that.numData();
        that.setData({
          telephone,
          codeFlag: true,
          error,
          isNum: true
        })
        wx.hideLoading()
      } else {
        telephone = new_phoneNum.substring(0, 3) + '****' + new_phoneNum.substring(7);
        that.numData();
        that.setData({
          telephone,
          codeFlag: true,
          error,
          isNum: true
        })
        wx.hideLoading()
        wx.showToast({
          title: '获取失败，请稍后再试',
          icon: 'none'
        })
        wx.hideLoading()
      }
    })

  },
  // 倒计时
  numData() {
    var timer = '',
      num = this.data.num,
      isNum = '',
      that = this;
    timer = setInterval(() => {
      num = num - 1;
      that.setData({
        num
      })
      if (num == 0) {
        clearInterval(timer);
        num = 60;
        isNum = false;
        that.setData({
          num,
          isNum
        })
      }
    }, 1000)
  },

  changeValue(e) {
    var flag = e.currentTarget.dataset.flag,
      color = '';
    if (flag == 'number') {
      color = this.data.new_phoneNum != '' ? 1 : 0
    } else if (flag == 'code') {
      color = this.data.code != '' ? 1 : 0
    }
    this.setData({
      color
    })
    console.log(this.data);
  },

  // 验证
  checkCode() {
    console.log(this.data.new_phoneNum);
    var url = app.globalData.url + 'member/updateTel',
      new_phoneNum = this.data.new_phoneNum;
    app.wxRequest(url, {
      flag: 0,
      member_id: app.globalData.memberId,
      member_tel: new_phoneNum
    }, res => {
      if (res.flag) {
        wx.showModal({
          title: '提示',
          content: '修改成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })



  },


  // 重新获取
  getAgain() {
    if (this.data.isNum == false) {
      this.getCode();
      this.isNum = true;
      this.numData();
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var old_phoneNum = app.globalData.member.memberTel
    this.setData({
      old_phoneNum
    })
    console.log(old_phoneNum);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})